package pl.plesken.graphic;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import org.apache.xmlbeans.XmlException;

import pl.plesken.tools.FileCode;
import pl.plesken.xml.ConfigParser;
import pl.plesken.xml.EditXMLFile;
import pl.plesken.xml.SetMoviesXML;

public class CreateCoverImage {

	private String file;
	private String img;
	private String skinHome;
	private String movieType;
	private int largeWidth;
	private int largeHeight;
	private int smallWidth;
	private int smallHeight;
	private String perspectiveDirection;
    private float perspectiveTopPoster;
    private float perspectiveBottomPoster;
    private float perspectiveTopThumbnail;
    private float perspectiveBottomThumbnail;
    private String posterReflectionHeight;
    private String thumbnailReflectionHeight;
	private boolean isHDLogo;
	private boolean isThumbHDLogo;
	private boolean isPostHDLogo;
    private boolean isTVLogo;
	private boolean isThumbTVLogo;
	private boolean isPostTVLogo;
    private boolean isLanguage;
    private boolean isPosterReflection;
    private boolean isThumbnailReflection;
    private boolean isPosterPerspective;
    private boolean isThumbnailPerspective;
    private boolean normalizePosters;
    private String thumbnailExt;
    private String posterExt;
    private String coverImg;
	private String coverType;
	private String videoOutput;
	private String starsImg;
	private int rating;
	private String logoHD;
	private String logoTV;
	private boolean isStars;
	private boolean isSet;
	private boolean isSetCover;
	private boolean isDisableMJB;
	private boolean HDLogo;
	private boolean TVLogo;
	private boolean isThumbPersp;
	private boolean isThumbRefl;
	private boolean isPostPersp;
	private boolean isPostRefl;
	private String nameSet;
	private String formatImg;

	public void generate(String dir, String baseName) {
		try {
			EditXMLFile xml = new EditXMLFile();
			GraphicTools cg = new GraphicTools();
			
			file = dir + File.separator + baseName + ".xml";
			img = dir + File.separator + baseName + ".jpg";
			skinHome = xml.skinDir(xml.loadData(file));
			movieType = xml.videoSource(xml.loadData(file));
			largeWidth = Integer.valueOf(xml.posterWidth(xml.loadData(file)));
			largeHeight = Integer.valueOf(xml.posterHeight(xml.loadData(file)));
			smallWidth = Integer.valueOf(xml.thumbnailWidth(xml.loadData(file)));
			smallHeight = Integer.valueOf(xml.thumbnailHeight(xml.loadData(file)));
			perspectiveDirection = xml.perspectiveDirection(xml.loadData(file));
			perspectiveTopPoster = Float.valueOf(xml.perspectiveTopPoster(xml.loadData(file)));
			perspectiveBottomPoster = Float.valueOf(xml.perspectiveBottomPoster(xml.loadData(file)));
			perspectiveTopThumbnail = Float.valueOf(xml.perspectiveTopThumbnail(xml.loadData(file)));
			perspectiveBottomThumbnail = Float.valueOf(xml.perspectiveBottomThumbnail(xml.loadData(file)));
			posterReflectionHeight = xml.posterReflectionHeight(xml.loadData(file));
			thumbnailReflectionHeight = xml.thumbnailReflectionHeight(xml.loadData(file));
			isHDLogo = xml.logoHD(xml.loadData(file));
			isTVLogo = xml.logoTV(xml.loadData(file));
			isLanguage = xml.logoLang(xml.loadData(file));
			isPosterReflection = xml.posterReflection(xml.loadData(file));
			isThumbnailReflection = xml.thumbnailReflection(xml.loadData(file));
			isPosterPerspective = Boolean.valueOf(xml.postersPerspective(xml.loadData(file)));
			isThumbnailPerspective = xml.perspectiveThumbnail(xml.loadData(file));
			thumbnailExt = xml.thumbnailFormat(xml.loadData(file));
			posterExt = xml.postersFormat(xml.loadData(file));
			
			BufferedImage bi = cg.loadImage(img);
			
			String largeOutput = dir + File.separator + baseName + "_large." + posterExt;
			String smallOutput = dir + File.separator + baseName + "_small." + thumbnailExt;

			BufferedImage largeIMG = cg.resizeImage(bi, largeWidth, largeHeight);
			BufferedImage smallIMG = cg.resizeImage(bi, smallWidth, smallHeight);
			
			if (isHDLogo) {
				cg.drawLogoHD(skinHome, largeIMG, isTVLogo, movieType);
				cg.drawLogoHD(skinHome, smallIMG, isTVLogo, movieType);
			}

			/*if (isTVLogo) {
				cg.drawLogoTV(skinHome, largeIMG, isHDLogo, movieType);
				cg.drawLogoTV(skinHome, smallIMG, isHDLogo, movieType);
			}*/

			if (isPosterReflection) {
				largeIMG = cg.createReflectedPicture(largeIMG, posterReflectionHeight);
			}
			
			if (isThumbnailReflection) {
				smallIMG = cg.createReflectedPicture(smallIMG, thumbnailReflectionHeight);
			}
			
			if (isPosterPerspective) {
				largeIMG = cg.create3DPicture(largeIMG, perspectiveDirection, perspectiveTopPoster, perspectiveBottomPoster);
			}

			if (isThumbnailPerspective) {
				smallIMG = cg.create3DPicture(smallIMG, perspectiveDirection, perspectiveTopThumbnail, perspectiveBottomThumbnail);
			}
			
			if (posterExt.equalsIgnoreCase("jpg") || posterExt.equalsIgnoreCase("jpeg")) {
				cg.saveImageAsJpg(largeIMG, largeOutput);
			}

			if (thumbnailExt.equalsIgnoreCase("jpg") || thumbnailExt.equalsIgnoreCase("jpeg")) {
				cg.saveImageAsJpg(smallIMG, smallOutput);
			}

			if (posterExt.equalsIgnoreCase("png")) {
				cg.saveImageAsPng(largeIMG, largeOutput);
			}

			if (thumbnailExt.equalsIgnoreCase("png")) {
				cg.saveImageAsPng(smallIMG, smallOutput);
			}
			
			
		} catch(IOException e) {
			e.printStackTrace();
		} catch(XmlException e) {
			e.printStackTrace();
		}
	}
	
	public void createCoverImage(String dir, String baseName) {
		try {
			EditXMLFile xml = new EditXMLFile();
			CreateCoverImage i = new CreateCoverImage();
			GraphicTools cg = new GraphicTools();
			ConfigParser cnf = new ConfigParser();
				
			file = dir + File.separator + baseName + ".xml";
			img = dir + File.separator + baseName + ".jpg";
		
			videoOutput = xml.videoOutput(xml.loadData(file));
			thumbnailExt = xml.thumbnailFormat(xml.loadData(file));
			posterExt = xml.postersFormat(xml.loadData(file));
			largeWidth = Integer.valueOf(xml.posterWidth(xml.loadData(file)));
			largeHeight = Integer.valueOf(xml.posterHeight(xml.loadData(file)));
			smallWidth = Integer.valueOf(xml.thumbnailWidth(xml.loadData(file)));
			smallHeight = Integer.valueOf(xml.thumbnailHeight(xml.loadData(file)));
			perspectiveDirection = xml.perspectiveDirection(xml.loadData(file));
			perspectiveTopPoster = Float.valueOf(xml.perspectiveTopPoster(xml.loadData(file)));
			perspectiveBottomPoster = Float.valueOf(xml.perspectiveBottomPoster(xml.loadData(file)));
			perspectiveTopThumbnail = Float.valueOf(xml.perspectiveTopThumbnail(xml.loadData(file)));
			perspectiveBottomThumbnail = Float.valueOf(xml.perspectiveBottomThumbnail(xml.loadData(file)));
			posterReflectionHeight = xml.posterReflectionHeight(xml.loadData(file));
			thumbnailReflectionHeight = xml.thumbnailReflectionHeight(xml.loadData(file));
			isLanguage = xml.logoLang(xml.loadData(file));
			isPosterReflection = xml.posterReflection(xml.loadData(file));
			isThumbnailReflection = xml.thumbnailReflection(xml.loadData(file));
			isPosterPerspective = Boolean.valueOf(xml.postersPerspective(xml.loadData(file)));
			isThumbnailPerspective = xml.perspectiveThumbnail(xml.loadData(file));
			isHDLogo = xml.logoHD(xml.loadData(file));
			isTVLogo = xml.logoTV(xml.loadData(file));
			isStars = Boolean.valueOf(cnf.readConf("thumbRating"));
			isSetCover = Boolean.valueOf(cnf.readConf("setCover"));
			isDisableMJB = Boolean.valueOf(cnf.readConf("disableMJB"));
			isThumbHDLogo = i.drawHDLogo(file, "thumb");
			isPostHDLogo = i.drawHDLogo(file, "post");
			isThumbTVLogo = i.drawTVLogo(file, "thumb");
			isPostTVLogo = i.drawTVLogo(file, "post");
			isThumbPersp = Boolean.valueOf(cnf.readConf("thumbPersp"));
			isThumbRefl = Boolean.valueOf(cnf.readConf("thumbReflection"));
			isPostPersp = Boolean.valueOf(cnf.readConf("postPersp"));
			isPostRefl = Boolean.valueOf(cnf.readConf("postReflection"));
			
			//Output files
			String largeOutput = dir + File.separator + baseName + "_large." + posterExt;
			String smallOutput = dir + File.separator + baseName + "_small." + thumbnailExt;
			String SetOutput = dir + File.separator + "Set_" + thumbnailExt;
			
			//Load image and cover and create mask
			BufferedImage bi = cg.loadImage(img);
			BufferedImage ci = cg.loadImage(i.coverTypeImage(dir, baseName));
			BufferedImage mask = cg.createMask(ci.getWidth(), ci.getHeight());
			
			int w = (int)(ci.getWidth() * 0.865);
			int h = (int)(ci.getHeight() * 0.951);
		
			bi = cg.resize(bi, w, h);
			bi = cg.addImage(bi, mask);
			bi = cg.coverImage(bi, ci);
			
			if (i.formatImage(dir, baseName) != null) {
				BufferedImage fm = cg.loadImage(i.formatImage(dir, baseName));
				bi = cg.addFormat(bi, fm);
			}
			
			BufferedImage largeIMG = cg.resize(bi, bi.getWidth(), bi.getHeight());;
			BufferedImage smallIMG = cg.resize(bi, bi.getWidth(), bi.getHeight());;
			//BufferedImage largeIMG = cg.resize(bi, largeWidth, largeHeight);
			//BufferedImage SetIMG = cg.resize(bi, (int)(bi.getWidth() * 0.95), (int)(bi.getHeight() * 0.95));
			
			if (isPostHDLogo) {
				if (i.coverHDLogo(dir, baseName) != null) {
					BufferedImage logo = cg.loadImage(i.coverHDLogo(dir, baseName));
					largeIMG = cg.addHDLogo(largeIMG, logo);
				}
			}
			
			if (isThumbHDLogo) {
				if (i.coverHDLogo(dir, baseName) != null) {
					BufferedImage logo = cg.loadImage(i.coverHDLogo(dir, baseName));
					smallIMG = cg.addHDLogo(smallIMG, logo);
				}
			}

			if (isPostTVLogo) {
				BufferedImage logo = cg.loadImage(i.coverTVLogo());
				largeIMG = cg.addTVLogo(largeIMG, logo);
			}
			
			if (isThumbTVLogo) {
				BufferedImage logo = cg.loadImage(i.coverTVLogo());
				smallIMG = cg.addTVLogo(smallIMG, logo);
			}
		
			if (isDisableMJB && isStars) {
				BufferedImage stars = cg.loadImage(i.ratingImage(dir, baseName));
				smallIMG = cg.addStars(smallIMG, stars);
			}
			
			if (((isDisableMJB == false) && isPosterReflection) || isPostRefl) {
				largeIMG = cg.createReflectedPicture(largeIMG, posterReflectionHeight);
			}
			
			if (((isDisableMJB == false) && isThumbnailReflection) || isThumbRefl) {
				smallIMG = cg.createReflectedPicture(smallIMG, thumbnailReflectionHeight);
			}
			
			if (((isDisableMJB == false) && isPosterPerspective) || isPostPersp) {
				largeIMG = cg.create3DPicture(largeIMG, perspectiveDirection, perspectiveTopPoster, perspectiveBottomPoster);
			}

			if (((isDisableMJB == false) && isThumbnailPerspective) || isThumbPersp) {
				smallIMG = cg.create3DPicture(smallIMG, perspectiveDirection, perspectiveTopThumbnail, perspectiveBottomThumbnail);
			}
							
			//largeIMG = cg.resize(largeIMG, largeWidth, largeHeight);
			smallIMG = cg.resize(smallIMG, smallWidth, smallHeight);
			
			if (posterExt.equalsIgnoreCase("jpg") || posterExt.equalsIgnoreCase("jpeg")) {
				cg.saveImageAsJpg(largeIMG, largeOutput);
			}

			if (thumbnailExt.equalsIgnoreCase("jpg") || thumbnailExt.equalsIgnoreCase("jpeg")) {
				cg.saveImageAsJpg(smallIMG, smallOutput);
			}

			if (posterExt.equalsIgnoreCase("png")) {
				cg.saveImageAsPng(largeIMG, largeOutput);
			}

			if (thumbnailExt.equalsIgnoreCase("png")) {
				cg.saveImageAsPng(smallIMG, smallOutput);
			}
			
		} catch(IOException e) {
			e.printStackTrace();
		} catch(XmlException e) {
			e.printStackTrace();
		}
	}

	public void createSetImage(String dir, String baseName) {
		try {
			SetMoviesXML xml = new SetMoviesXML();
			CreateCoverImage i = new CreateCoverImage();
			GraphicTools cg = new GraphicTools();
			ConfigParser cnf = new ConfigParser();
			FileCode code = new FileCode();
			
			file = dir + File.separator + baseName + ".xml";
			String[] fileIN = xml.getThumbPicture(xml.loadData(file)).split("_small.");
			//img = dir + File.separator + baseName + ".jpg";
			
			thumbnailExt = xml.thumbnailFormat(xml.loadData(file));
			smallWidth = Integer.valueOf(xml.thumbnailWidth(xml.loadData(file)));
			smallHeight = Integer.valueOf(xml.thumbnailHeight(xml.loadData(file)));
			perspectiveDirection = xml.perspectiveDirection(xml.loadData(file));
			perspectiveTopThumbnail = Float.valueOf(xml.perspectiveTopThumbnail(xml.loadData(file)));
			perspectiveBottomThumbnail = Float.valueOf(xml.perspectiveBottomThumbnail(xml.loadData(file)));
			thumbnailReflectionHeight = xml.thumbnailReflectionHeight(xml.loadData(file));
			isThumbnailReflection = xml.thumbnailReflection(xml.loadData(file));
			isThumbnailPerspective = xml.perspectiveThumbnail(xml.loadData(file));
			isDisableMJB = Boolean.valueOf(cnf.readConf("disableMJB"));
			//isThumbHDLogo = i.drawHDLogo(file, "thumb");
			//isThumbTVLogo = i.drawTVLogo(file, "thumb");
			isThumbPersp = Boolean.valueOf(cnf.readConf("thumbPersp"));
			isThumbRefl = Boolean.valueOf(cnf.readConf("thumbReflection"));
			
			//String fileXML =  
			img = dir + File.separator + code.decode(fileIN[0]) + ".jpg";
			
			//Output file
			String setOutput = dir + File.separator + baseName + "_small." + thumbnailExt; 
			//System.out.println("zapisany plik: " + setOutput);
			
			//Load image and cover and create mask
			BufferedImage bi = cg.loadImage(img);
			BufferedImage ci = cg.loadImage("pictures" + File.separator + "Covers" + File.separator + "CoverBlack.png");
			BufferedImage logoSet = cg.loadImage("pictures" + File.separator + "Covers" + File.separator + "lc_Set.png");
			BufferedImage mask = cg.createMask(ci.getWidth(), ci.getHeight());
			
			int w = (int)(ci.getWidth() * 0.865);
			int h = (int)(ci.getHeight() * 0.951);
		
			bi = cg.resize(bi, w, h);
			bi = cg.addImage(bi, mask);
			bi = cg.coverImage(bi, ci);
			
			bi = cg.addTVLogo(bi, logoSet);
			
/*			if (isThumbHDLogo) {
				if (i.coverHDLogo(dir, baseName) != null) {
					BufferedImage logo = cg.loadImage(i.coverHDLogo(dir, baseName));
					bi = cg.addHDLogo(bi, logo);
				}
			}
			
			if (isThumbTVLogo) {
				BufferedImage logo = cg.loadImage(i.coverTVLogo());
				bi = cg.addTVLogo(bi, logo);
			}*/
		
			/*if (isDisableMJB && isStars) {
				BufferedImage stars = cg.loadImage(i.ratingImage(dir, baseName));
				bi = cg.addStars(bi, stars);
			}*/
			
			BufferedImage SetMask = cg.createMask(bi.getWidth(), bi.getHeight());
			BufferedImage SetIMG1 = cg.resize(bi, (int)(bi.getWidth() * 0.92), (int)(bi.getHeight() * 0.92));
			BufferedImage SetIMG2 = cg.resize(bi, (int)(bi.getWidth() * 0.92), (int)(bi.getHeight() * 0.92));
			BufferedImage SetIMG3 = cg.resize(bi, (int)(bi.getWidth() * 0.92), (int)(bi.getHeight() * 0.92));
			
			SetIMG1 = cg.addSetImage(SetIMG1, SetMask, (int)(SetMask.getWidth()/2 - SetIMG1.getWidth()/2 - 20), (int)(SetMask.getHeight() - SetIMG1.getHeight() - 45));
			SetIMG2 = cg.addSetImage(SetIMG2, SetMask, (int)(SetMask.getWidth()/2 - SetIMG2.getWidth()/2), (int)(SetMask.getHeight() - SetIMG2.getHeight() - 20));
			SetIMG3 = cg.addSetImage(SetIMG3, SetMask, (int)(SetMask.getWidth()/2 - SetIMG3.getWidth()/2 + 20), (int)(SetMask.getHeight() - SetIMG3.getHeight() + 5));
			
			BufferedImage SetIMG = SetIMG3;

			SetIMG = cg.resize(SetIMG, smallWidth, smallHeight);
			
			
			if (((isDisableMJB == false) && isThumbnailReflection) || isThumbRefl) {
				SetIMG = cg.createReflectedPicture(SetIMG, thumbnailReflectionHeight);
			}
			
			if (((isDisableMJB == false) && isThumbnailPerspective) || isThumbPersp) {
				SetIMG = cg.create3DPicture(SetIMG, perspectiveDirection, perspectiveTopThumbnail, perspectiveBottomThumbnail);
			}
			
			if (thumbnailExt.equalsIgnoreCase("jpg") || thumbnailExt.equalsIgnoreCase("jpeg")) {
				cg.saveImageAsJpg(SetIMG, setOutput);
			}
			
			if (thumbnailExt.equalsIgnoreCase("png")) {
				cg.saveImageAsPng(SetIMG, setOutput);
			}
			
		} catch(IOException e) {
			e.printStackTrace();
		} catch(XmlException e) {
			e.printStackTrace();
		}
	}
	
	public String ratingImage(String dir, String baseName) throws IOException, XmlException {
		EditXMLFile xml = new EditXMLFile();
		
		file = dir + File.separator + baseName + ".xml";
		rating = Integer.valueOf(xml.rating(xml.loadData(file)));
		
		if (rating == 0 || rating < 10) {
			starsImg = "pictures" + File.separator + "Covers" + File.separator + "rating_0.png";
		} else if (rating == 10 || rating < 20) {
			starsImg = "pictures" + File.separator + "Covers" + File.separator + "rating_10.png";
		} else if (rating == 20 || rating < 30) {
			starsImg = "pictures" + File.separator + "Covers" + File.separator + "rating_20.png";
		} else if (rating == 30 || rating < 40) {
			starsImg = "pictures" + File.separator + "Covers" + File.separator + "rating_30.png";
		} else if (rating == 40 || rating < 50) {
			starsImg = "pictures" + File.separator + "Covers" + File.separator + "rating_40.png";
		} else if (rating == 50 || rating < 60) {
			starsImg = "pictures" + File.separator + "Covers" + File.separator + "rating_50.png";
		} else if (rating == 60 || rating < 70) {
			starsImg = "pictures" + File.separator + "Covers" + File.separator + "rating_60.png";
		} else if (rating == 70 || rating < 80) {
			starsImg = "pictures" + File.separator + "Covers" + File.separator + "rating_70.png";
		} else if (rating == 80 || rating < 90) {
			starsImg = "pictures" + File.separator + "Covers" + File.separator + "rating_80.png";
		} else if (rating == 90 || rating < 100) {
			starsImg = "pictures" + File.separator + "Covers" + File.separator + "rating_90.png";
		} else if (rating == 100) {
			starsImg = "pictures" + File.separator + "Covers" + File.separator + "rating_100.png";
		}
		
		return starsImg;
	}
	
	public String coverTypeImage(String dir, String baseName) throws IOException, XmlException {
		EditXMLFile xml = new EditXMLFile();

		file = dir + File.separator + baseName + ".xml";
		coverType = xml.videoSource(xml.loadData(file));
		videoOutput = xml.videoOutput(xml.loadData(file));
			
		if (coverType.toUpperCase().contains("HDDVD") || coverType.toUpperCase().contains("HD-DVD") || coverType.toUpperCase().contains("HD DVD")) {
			coverImg = "pictures" + File.separator + "Covers" + File.separator + "BoxHDDVD.png";
		} else if (coverType.toUpperCase().contains("BLU")) {
			coverImg = "pictures" + File.separator + "Covers" + File.separator + "BoxBluRay.png";
		} else if (coverType.toUpperCase().contains("HDTV")) {
			coverImg = "pictures" + File.separator + "Covers" + File.separator + "hdtvcase.png";
		} else if (coverType.toUpperCase().contains("DVD") && !coverType.toUpperCase().contains("HD")) {
			coverImg = "pictures" + File.separator + "Covers" + File.separator + "BoxDVD.png";
		} else if (coverType.toUpperCase().contains("DTHEATER")) {
			coverImg = "pictures" + File.separator + "Covers" + File.separator + "BoxDTheater.png";
		} else if (coverType.toUpperCase().contains("TV") && !coverType.toUpperCase().contains("HDTV")) {
			coverImg = "pictures" + File.separator + "Covers" + File.separator + "BoxTvcase.png";
		} else if (videoOutput.toUpperCase().contains("720") || videoOutput.toUpperCase().contains("1080")) {
			coverImg = "pictures" + File.separator + "Covers" + File.separator + "CoverBlue.png";
		} else {
			coverImg = "pictures" + File.separator + "Covers" + File.separator + "CoverClear.png";
		}
		
		return coverImg;
	}

	public String coverHDLogo(String dir, String baseName) throws IOException, XmlException {
		EditXMLFile xml = new EditXMLFile();

		file = dir + File.separator + baseName + ".xml";
		videoOutput = xml.videoOutput(xml.loadData(file));
				
		if (videoOutput.toUpperCase().contains("1080")) {
			logoHD = "pictures" + File.separator + "Covers" + File.separator + "rc_hd1080.png";
		}
		if (videoOutput.toUpperCase().contains("720")) {
			logoHD = "pictures" + File.separator + "Covers" + File.separator + "rc_hd720.png";
		}

		return logoHD;
	}
	
	public String coverTVLogo() throws IOException, XmlException {
		
		logoTV = "pictures" + File.separator + "Covers" + File.separator + "tvshow.png";
		
		return logoTV;
	}
	
	public Boolean drawHDLogo(String file, String type) throws IOException, XmlException {
		ConfigParser cnf = new ConfigParser();
		EditXMLFile xml = new EditXMLFile();
		
		isDisableMJB = Boolean.valueOf(cnf.readConf("disableMJB"));
		
		if (isDisableMJB) {
			if (type.equalsIgnoreCase("thumb")) { 
				if (Boolean.valueOf(cnf.readConf("thumbHDlogo"))) {
					//HDLogo = Boolean.valueOf(cnf.readConf("thumbHDlogo"));
					HDLogo = xml.logoThumbHD(xml.loadData(file));
				} else {
					HDLogo = false;
				}
			} else if (type.equalsIgnoreCase("post")) { 
				if (Boolean.valueOf(cnf.readConf("postHDlogo"))) {
					HDLogo = xml.logoHD(xml.loadData(file));
				} else {
					HDLogo = false;
				}
			} else {
				HDLogo = false;
			}
		} else {
			if (type.equalsIgnoreCase("thumb")) { 
				HDLogo = xml.logoThumbHD(xml.loadData(file));
			} else if (type.equalsIgnoreCase("post")) { 
				HDLogo = xml.logoHD(xml.loadData(file));
			} else {
				HDLogo = false;
			}
		}

		return HDLogo;
	}

	public Boolean drawTVLogo(String file, String type) throws IOException, XmlException {
		ConfigParser cnf = new ConfigParser();
		EditXMLFile xml = new EditXMLFile();
		
		isDisableMJB = Boolean.valueOf(cnf.readConf("disableMJB"));
		
		if (isDisableMJB) {
			if (type.equalsIgnoreCase("thumb")) { 
				if (Boolean.valueOf(cnf.readConf("thumbTVlogo"))) {
					if (!xml.season(xml.loadData(file)).equals("-1") || xml.logoThumbTV(xml.loadData(file))) {
						//TVLogo = Boolean.valueOf(cnf.readConf("thumbTVlogo"));
						TVLogo = true;
					} else {
						TVLogo = false;
					}
				}
			} else if (type.equalsIgnoreCase("post")) {
				if (Boolean.valueOf(cnf.readConf("postTVlogo"))) {
					TVLogo = Boolean.valueOf(cnf.readConf("thumbTVlogo"));
				} else {
					TVLogo = false;
				}
			} else {
				TVLogo = false;
			}
			
		} else {
			if (type.equalsIgnoreCase("thumb")) { 
				TVLogo = xml.logoThumbTV(xml.loadData(file));
			}
			if (type.equalsIgnoreCase("post")) { 
				TVLogo = xml.logoTV(xml.loadData(file));
			} else {
				TVLogo = false;
			}
		}
		
		return TVLogo;
	}

	public String formatImage(String dir, String baseName) throws IOException, XmlException {
		EditXMLFile xml = new EditXMLFile();

		file = dir + File.separator + baseName + ".xml";
		String format = xml.videoCodec(xml.loadData(file));
		String container = xml.container(xml.loadData(file));
		
		if (format.toUpperCase().contains("MICROSOFT") && container.toUpperCase().contains("WINDOWS MEDIA")) {
			formatImg = "pictures" + File.separator + "Covers" + File.separator + "thumb-Wmv.png";
		} else if (format.toUpperCase().contains("V_MPEG4/ISO/AVC") && container.toUpperCase().contains("MATROSKA")) {
			formatImg = "pictures" + File.separator + "Covers" + File.separator + "thumb-Mkv.png";
		} else if (format.toUpperCase().contains("XVID") && container.toUpperCase().contains("AVI")) {
			formatImg = "pictures" + File.separator + "Covers" + File.separator + "thumb-XviD.png";
		} else if (format.toUpperCase().contains("DIVX") && container.toUpperCase().contains("AVI")) {
			formatImg = "pictures" + File.separator + "Covers" + File.separator + "thumb-DivX.png";
		} else if (format.toUpperCase().contains("AVC") && container.toUpperCase().contains("MPEG-TS")) {
			formatImg = "pictures" + File.separator + "Covers" + File.separator + "thumb-ts.png";
		} else if (format.toUpperCase().contains("AVC") && container.toUpperCase().contains("BDAV")) {
			formatImg = "pictures" + File.separator + "Covers" + File.separator + "thumb-Blu-ray.png";
		} else if (format.toUpperCase().contains("VC-1") && container.toUpperCase().contains("MPEG-TS")) {
			formatImg = "pictures" + File.separator + "Covers" + File.separator + "thumb-MS.png";
		} else if (format.toUpperCase().contains("MPEG VIDEO") && container.toUpperCase().contains("BDAV")) {
			formatImg = "pictures" + File.separator + "Covers" + File.separator + "thumb-Blu-ray.png";
		} else if (format.toUpperCase().contains("MICROSOFT") && container.toUpperCase().contains("MATROSKA")) {
			formatImg = "pictures" + File.separator + "Covers" + File.separator + "thumb-Mkv.png";
		}
		//System.out.println("zwrocony format: " + formatImg);
		return formatImg;
	}
	
}
